﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TcomInvoice
{
    public partial class Main : Form
    {
        private bool isDirty = false;
        private int? currentInvoiceID = null;
        private int? currentCustomerID = null;
        private DataHelper db = new DataHelper();
        private bool isEditingQty = false;
        private Control[] editControl = new Control[5];
        private bool isPrinted = false;

        public Main()
        {
            InitializeComponent();
        }

        #region Form Events
        private void Main_Load(object sender, EventArgs e)
        {
            InitControlArray();

            InitDataSource();
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isDirty && (currentCustomerID != null || currentInvoiceID != null || !isPrinted))
            {
                DialogResult result = MessageBox.Show("Lưu lại hóa đơn dang dở?", "Save invoice confirmation", MessageBoxButtons.YesNoCancel);
                switch (result)
                {
                    case System.Windows.Forms.DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                    case System.Windows.Forms.DialogResult.Yes:
                        SaveSettings();
                        SaveDictionary();
                        SaveCustomerInfo();
                        SaveInvoice(false);
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion

        #region Init data helpers
        private void InitDataSource()
        {
            InitSuggestionDataForControl(cboItem, DataHelper.EnumDataDictionaryCode.Item.ToString());
            InitSuggestionDataForControl(cboUnit, DataHelper.EnumDataDictionaryCode.Unit.ToString());
            InitSuggestionDataForControl(cboVAT, DataHelper.EnumDataDictionaryCode.VAT.ToString());
            InitSuggestionDataForControl(cboTcomAccount, DataHelper.EnumDataDictionaryCode.TcomAccount.ToString());
            InitSuggestionDataForControl(txtName, "Presentative");
            InitSuggestionDataForControl(txtOrg, "Organization");
            InitSuggestionDataForControl(txtTaxCode, "TaxCode");
            InitSuggestionDataForControl(txtTelNo, "Telephone");
            InitSuggestionDataForControl(txtFaxNo, "Fax");
            InitSuggestionDataForControl(cboPaymentMethod, DataHelper.EnumDataDictionaryCode.Paymethod.ToString());
            InitSuggestionDataForControl(txtAddress, "Address");
            InitSuggestionDataForControl(txtAccount, "Account");
            InitSuggestionDataForControl(txtBranch, "Branch");

            //Load setting
            LoadSettings();
        }

        private void InitSuggestionDataForControl(Control ctl, string code, string displayMember = "Value")
        {
            IEnumerable<string> list = db.GetSuggestionList(code);
            if (list != null)
            {
                if (ctl is TextBox)
                {
                    (ctl as TextBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    (ctl as TextBox).AutoCompleteSource = AutoCompleteSource.CustomSource;
                    (ctl as TextBox).AutoCompleteCustomSource.AddRange(list.ToArray());
                }
                if (ctl is ComboBox)
                {
                    (ctl as ComboBox).DataSource = list.ToList();
                    (ctl as ComboBox).DisplayMember = displayMember;
                }
            }
        }

        private void InitControlArray()
        {
            editControl[0] = null;
            editControl[1] = cboItem;
            editControl[2] = cboUnit;
            editControl[3] = textedit;
            editControl[4] = textedit;
        } 
        #endregion

        #region Button Actions
        private void btnDataReview_Click(object sender, EventArgs e)
        {
            ListInvoice frm = new ListInvoice();
            frm.ShowDialog();
            InitDataSource();
            if (frm.SelectInvoiceID != null)
            {
                ReloadInvoice(frm.SelectInvoiceID.Value);
            }
        }

        private void btnFindCustomer_Click(object sender, EventArgs e)
        {
            SearchCustomer search = new SearchCustomer();
            search.ShowDialog();
            if (search.SelectCustomer != null)
            {
                currentCustomerID = search.SelectCustomer.CustomerID;

                txtName.Text = search.SelectCustomer.PresentativeName;
                txtOrg.Text = search.SelectCustomer.Organization;
                txtTaxCode.Text = search.SelectCustomer.TaxCode;
                txtAddress.Text = search.SelectCustomer.Address;
                txtTelNo.Text = search.SelectCustomer.TelNo;
                txtFaxNo.Text = search.SelectCustomer.FaxNo;
                txtAccount.Text = search.SelectCustomer.BankAccount;
                txtBranch.Text = search.SelectCustomer.BankBranch;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (chkPrintFreeStyle.Checked)
            {
                //if (currentInvoiceID == null) //New Invoice
                //{
                //    NewInvoice(true);
                //}
                //else
                //{
                //    UpdateInvoice(true);
                //}
                NewInvoice(true);
                PrintInvoice();
            }
            else
            {
                SaveSettings();

                RemoveUnusedLines();

                if (!CheckValidationEverything()) return;

                SaveInvoice(true);
                InitDataSource();

                PrintInvoice();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            SaveSettings();

            if (isDirty)
            {
                DialogResult result = MessageBox.Show("Lưu lại hóa đơn dang dở?", "Save invoice confirmation", MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    RemoveUnusedLines();

                    if (!CheckValidationEverything()) return;

                    SaveInvoice(false);
                    InitDataSource();
                }
            }

            ClearForm();

            currentInvoiceID = null;
            currentCustomerID = null;
        }

        private void btnSaveAndClose_Click(object sender, EventArgs e)
        {
            this.Close();
        } 
        #endregion

        #region ListView events
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listView1.Items.Count == 10)
            {
                MessageBox.Show("Hóa đơn chỉ có thể in 10 dòng.");
                return;
            }
            if (listView1.Items.Count == 0 || listView1.GetItemAt(e.X, e.Y) == null)
            {
                ListViewItem item = listView1.Items.Add(new ListViewItem(ReindexInvoiceLine()));
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "" }); //ItemName
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "" }); //UnitName
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "1" }); //Quantity
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "0" }); //Price
                item.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "0" }); //Amount
            }
        }

        private void listView1_SubItemClicked(object sender, ListViewEx.SubItemEventArgs e)
        {
            isEditingQty = false;
            if (e.SubItem != 0 && e.SubItem != 5)
            {
                isEditingQty = (e.SubItem == 3);
                listView1.StartEditing(editControl[e.SubItem], e.Item, e.SubItem);
            }
        }

        private void listView1_SubItemEndEditing(object sender, ListViewEx.SubItemEndEditingEventArgs e)
        {
            switch (e.SubItem)
            {
                case 3:
                    e.Item.SubItems[5].Text = (Utility.GetDecimal(e.DisplayText) * Utility.GetDecimal(e.Item.SubItems[4].Text)).ToString("#,##");
                    e.DisplayText = Utility.GetDecimal(e.DisplayText).ToString("#,##");
                    break;
                case 4:
                    e.Item.SubItems[5].Text = (Utility.GetDecimal(e.DisplayText) * Utility.GetDecimal(e.Item.SubItems[3].Text)).ToString("#,##");
                    e.DisplayText = Utility.GetDecimal(e.DisplayText).ToString("#,##");
                    break;
                case 5:
                case 1:
                case 2:
                case 0:
                default:
                    break;
            }

            UpdateTotal();
        }

        private void listView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    listView1.Items.Remove(item);
                }
            }
        } 
        #endregion

        #region Control events
        private void cboVAT_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateTotal();
        }

        private void cboVAT_TextChanged(object sender, EventArgs e)
        {
            UpdateTotal();
        }

        private void textedit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Control)
            {
                textedit.Text = Utility.AddThousand(textedit.Text);
            }

            if (isEditingQty)
            {
                if (e.KeyCode == Keys.Up)
                {
                    textedit.Text = (Utility.GetDecimal(textedit.Text) + 1).ToString("#,##");
                }
                if (e.KeyCode == Keys.Down)
                {
                    textedit.Text = (Utility.GetDecimal(textedit.Text) - 1).ToString("#,##");
                }
            }
            else
            {
                if (e.KeyCode == Keys.Up)
                {
                    textedit.Text = (Utility.GetDecimal(textedit.Text) + 500).ToString("#,##");
                }
                if (e.KeyCode == Keys.Down)
                {
                    textedit.Text = (Utility.GetDecimal(textedit.Text) - 500).ToString("#,##");
                }
            }

        }

        private void txtInvoiceNo_Leave(object sender, EventArgs e)
        {
            txtInvoiceNo.Text = txtInvoiceNo.Text.PadLeft(7, '0');
        }

        private void control_keydown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        } 
        #endregion

        #region Helpers
        private string ReindexInvoiceLine()
        {
            int index = 1;
            foreach (ListViewItem item in listView1.Items)
            {
                item.Text = index.ToString();
                index++;
            }
            return index.ToString();
        }

        private void RemoveUnusedLines()
        {
            //Remove unused lines
            foreach (ListViewItem item in listView1.Items)
            {
                if (string.IsNullOrEmpty(item.SubItems[1].Text))
                {
                    listView1.Items.Remove(item);
                }
            }
        }

        private void ClearForm()
        {
            foreach (Control clt in this.Controls)
            {
                if (clt is TextBox) ((TextBox)clt).Clear();
                if (clt is ComboBox) ((ComboBox)clt).Text = "";
                if (clt is GroupBox)
                {
                    foreach (Control subclt in ((GroupBox)clt).Controls)
                    {
                        if (subclt is TextBox) ((TextBox)subclt).Clear();
                        if (subclt is ComboBox) ((ComboBox)subclt).Text = "";
                    }
                }
            }

            listView1.Items.Clear();
            isPrinted = false;
            isDirty = false;
        }

        private void SaveCustomerInfo()
        {
            if (currentCustomerID == null || db.LoadCustomer(currentCustomerID.Value) == null)
            {
                currentCustomerID = db.AddCustomer(txtName.Text, txtOrg.Text, txtTaxCode.Text,
                                                   txtAddress.Text, "", txtAccount.Text, txtBranch.Text);
            }
            else
            {
                db.UpdateCustomer(currentCustomerID.Value, txtName.Text, txtOrg.Text, txtTaxCode.Text,
                                  txtAddress.Text, "", txtAccount.Text, txtBranch.Text);
            }
        }

        private void SaveInvoice(bool IsPrinted)
        {
            try
            {
                SaveCustomerInfo();
                SaveDictionary();
                if (currentInvoiceID == null) //New Invoice
                {
                    NewInvoice(IsPrinted);
                }
                else
                {
                    UpdateInvoice(IsPrinted);
                }
            }
            catch { }
        }

        private void NewInvoice(bool IsPrinted)
        {
            decimal? sum = null;
            if (!string.IsNullOrEmpty(txtSumAmount.Text)) sum = decimal.Parse(txtSumAmount.Text);

            decimal? vat = null;
            if (!string.IsNullOrEmpty(cboVAT.Text)) vat = decimal.Parse(cboVAT.Text.Replace("%", ""));

            decimal? total = null;
            if (!string.IsNullOrEmpty(txtTotal.Text)) total = decimal.Parse(txtTotal.Text);

            currentInvoiceID = db.AddInvoiceHeader(currentCustomerID,
                                   txtInvoiceNo.Text, cboTcomAccount.Text, dtInvoiceDate.Value, cboPaymentMethod.Text,
                                   sum, vat, total, IsPrinted);
            foreach (ListViewItem item in listView1.Items)
            {
                if (listView1.CheckBoxes && !item.Checked)
                {
                    db.AddInvoiceLines(currentInvoiceID.Value, int.Parse(item.Text), string.Empty, string.Empty, 0, 0, 0);
                }
                else
                {
                    db.AddInvoiceLines(currentInvoiceID.Value, int.Parse(item.Text),
                                        item.SubItems[1].Text,
                                        item.SubItems[2].Text,
                                        string.IsNullOrEmpty(item.SubItems[3].Text) ? 0 : int.Parse(item.SubItems[3].Text),
                                        string.IsNullOrEmpty(item.SubItems[4].Text) ? 0 : decimal.Parse(item.SubItems[4].Text),
                                        string.IsNullOrEmpty(item.SubItems[5].Text) ? 0 : decimal.Parse(item.SubItems[5].Text));
                }

                SaveDictionaryForInvLine(item);
            }
        }

        private void UpdateInvoice(bool IsPrinted)
        {
            decimal? sum = null;
            if (!string.IsNullOrEmpty(txtSumAmount.Text)) sum = decimal.Parse(txtSumAmount.Text);

            decimal? vat = null;
            if (!string.IsNullOrEmpty(cboVAT.Text)) vat = decimal.Parse(cboVAT.Text.Replace("%", ""));

            decimal? total = null;
            if (!string.IsNullOrEmpty(txtTotal.Text)) total = decimal.Parse(txtTotal.Text);

            db.UpdateInvoiceHeader(currentInvoiceID.Value, currentCustomerID,
                                   txtInvoiceNo.Text, cboTcomAccount.Text, dtInvoiceDate.Value, cboPaymentMethod.Text,
                                   sum, vat, total, IsPrinted);
            db.RemoveAllInvoiceLines(currentInvoiceID.Value);
            foreach (ListViewItem item in listView1.Items)
            {
                db.AddInvoiceLines(currentInvoiceID.Value, int.Parse(item.Text),
                                    item.SubItems[1].Text,
                                    item.SubItems[2].Text,
                                    string.IsNullOrEmpty(item.SubItems[3].Text) ? 0 : int.Parse(item.SubItems[3].Text),
                                    string.IsNullOrEmpty(item.SubItems[4].Text) ? 0 : decimal.Parse(item.SubItems[4].Text),
                                    string.IsNullOrEmpty(item.SubItems[5].Text) ? 0 : decimal.Parse(item.SubItems[5].Text));

                SaveDictionaryForInvLine(item);
            }
        }

        private void SaveDictionary()
        {
            db.AddDataDictionary(DataHelper.EnumDataDictionaryCode.Paymethod, cboPaymentMethod.Text);
            db.AddDataDictionary(DataHelper.EnumDataDictionaryCode.TcomAccount, cboTcomAccount.Text);
            db.AddDataDictionary(DataHelper.EnumDataDictionaryCode.VAT, cboVAT.Text);
        }

        private void SaveDictionaryForInvLine(ListViewItem item)
        {
            db.AddDataDictionary(DataHelper.EnumDataDictionaryCode.Item, item.SubItems[1].Text);
            db.AddDataDictionary(DataHelper.EnumDataDictionaryCode.Unit, item.SubItems[2].Text);
        }

        private void UpdateTotal()
        {
            decimal total = 0;
            foreach (ListViewItem item in listView1.Items)
            {
                if (!string.IsNullOrEmpty(item.SubItems[5].Text))
                {
                    total += Utility.GetDecimal(item.SubItems[5].Text);
                }
            }
            txtSumAmount.Text = total.ToString("#,##");
            if (!string.IsNullOrEmpty(cboVAT.Text))
            {
                txtVATAmount.Text = (((Utility.GetDecimal(cboVAT.Text.Replace("%", ""))) / 100) * Utility.GetDecimal(total.ToString())).ToString("#,##");
            }
            if (!string.IsNullOrEmpty(txtSumAmount.Text) && !string.IsNullOrEmpty(txtVATAmount.Text))
            {
                txtTotal.Text = Math.Round(Utility.GetDecimal(txtSumAmount.Text) + Utility.GetDecimal(txtVATAmount.Text), 0).ToString("#,##");
            }
        }

        private void ReloadInvoice(int invoiceID)
        {
            currentInvoiceID = invoiceID;
            Invoice inv = db.LoadInvoiceHeader(currentInvoiceID.Value);
            if (inv != null)
            {
                ClearForm();

                currentCustomerID = inv.CustomerID;
                txtInvoiceNo.Text = inv.InvoiceNo;
                dtInvoiceDate.Value = inv.Date.Value;
                cboTcomAccount.Text = inv.AccNo;
                cboPaymentMethod.Text = inv.PayMethod;
                cboVAT.Text = !inv.VATPercent.HasValue ? "" : inv.VATPercent.Value.ToString() + "%";

                if (currentCustomerID != null && currentCustomerID.Value != -1)
                {
                    Customer selectCustomer = db.LoadCustomer(currentCustomerID.Value);
                    if (selectCustomer != null)
                    {
                        txtName.Text = selectCustomer.PresentativeName;
                        txtOrg.Text = selectCustomer.Organization;
                        txtTaxCode.Text = selectCustomer.TaxCode;
                        txtAddress.Text = selectCustomer.Address;
                        txtTelNo.Text = selectCustomer.TelNo;
                        txtFaxNo.Text = selectCustomer.FaxNo;
                        txtAccount.Text = selectCustomer.BankAccount;
                        txtBranch.Text = selectCustomer.BankBranch;
                    }
                }

                IEnumerable<InvoiceLine> lstInvoiceLines = db.LoadInvoiceLine(currentInvoiceID.Value);
                foreach (InvoiceLine invLine in lstInvoiceLines)
                {
                    ListViewItem item = listView1.Items.Add(invLine.LineNo.ToString());
                    item.SubItems.Add(invLine.ItemName);
                    item.SubItems.Add(invLine.UnitName);
                    item.SubItems.Add(invLine.Quantity.Value.ToString("#,##"));
                    item.SubItems.Add(invLine.Price.Value.ToString("#,##"));
                    item.SubItems.Add(invLine.Amount.Value.ToString("#,##"));
                }

                UpdateTotal();

                isPrinted = inv.IsPrinted ?? false;
                isDirty = false;
            }
        }

        private bool CheckValidationEverything()
        {
            if (string.IsNullOrEmpty(cboTcomAccount.SelectedItem.ToString()))
            {
                MessageBox.Show("Hãy nhập tài khoản.");
                cboTcomAccount.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtName.Text) && string.IsNullOrEmpty(txtOrg.Text)
                && string.IsNullOrEmpty(txtTaxCode.Text) && string.IsNullOrEmpty(txtAddress.Text)
                && string.IsNullOrEmpty(cboPaymentMethod.Text) && string.IsNullOrEmpty(txtAccount.Text)
                && string.IsNullOrEmpty(txtBranch.Text))
            {
                MessageBox.Show("Hãy nhập thông tin người mua.");
                txtName.Focus();
                return false;
            }

            foreach (ListViewItem item in listView1.Items)
            {
                if (string.IsNullOrEmpty(item.SubItems[1].Text)
                    || string.IsNullOrEmpty(item.SubItems[2].Text)
                    || string.IsNullOrEmpty(item.SubItems[3].Text)
                    || string.IsNullOrEmpty(item.SubItems[4].Text))
                {
                    MessageBox.Show("Thông tin hàng bán ở dòng #" + item.SubItems[0].Text + " chưa đầy đủ.");
                    return false;
                }

                double tmp = 0;
                if (!double.TryParse(item.SubItems[3].Text, out tmp))
                {
                    MessageBox.Show("Dòng #" + item.SubItems[0].Text + ": Số lượng nhập phải nhập số.");
                    return false;
                }

                if (!double.TryParse(item.SubItems[4].Text, out tmp))
                {
                    MessageBox.Show("Dòng #" + item.SubItems[0].Text + ": Giá nhập phải nhập số.");
                    return false;
                }
            }

            if (string.IsNullOrEmpty(cboVAT.SelectedItem.ToString()))
            {
                MessageBox.Show("Hãy nhập VAT.");
                cboVAT.Focus();
                return false;
            }

            return true;
        }

        private void PrintInvoice()
        {
            if (currentInvoiceID == null)
            {
                MessageBox.Show("Hoá đơn chưa nhập đủ thông tin.");
                return;
            }

            ViewRpt frm = new ViewRpt();
            frm.InvoiceID = currentInvoiceID.Value;
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintInvoiceHeader.Checked))
            {
                frm.InvoiceDate = dtInvoiceDate.Value;
                frm.TComAccount = cboTcomAccount.Text;
            }
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintCustomerInfo1.Checked))
            {
                frm.CustomerName = txtName.Text;
            }
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintCustomerInfo2.Checked))
            {
                frm.Organization = txtOrg.Text;
            }
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintCustomerInfo3.Checked))
            {
                frm.TaxCode = txtTaxCode.Text;
            }
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintCustomerInfo4.Checked))
            {
                frm.Address = txtAddress.Text;
            }
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintCustomerInfo5.Checked))
            {
                frm.PayMethod = cboPaymentMethod.Text;
                frm.Account = txtAccount.Text + (string.IsNullOrEmpty(txtBranch.Text) ? "" : " - " + txtBranch.Text);
            }
            if (!chkPrintFreeStyle.Checked || (chkPrintFreeStyle.Checked && chkPrintInvoiceFooter.Checked))
            {
                frm.SumAmount = string.IsNullOrEmpty(txtSumAmount.Text) ? 0 : double.Parse(txtSumAmount.Text);
                frm.VATPercent = string.IsNullOrEmpty(cboVAT.Text) ? 0 : double.Parse(cboVAT.Text.Replace("%", ""));
                frm.VATAmount = string.IsNullOrEmpty(txtVATAmount.Text) ? 0 : double.Parse(txtVATAmount.Text);
                frm.TotalAmount = string.IsNullOrEmpty(txtTotal.Text) ? 0 : double.Parse(txtTotal.Text);
            }
            frm.ShowDialog();

            isPrinted = true;
        }

        private void LoadSettings()
        {
            SettingsHelper sh = new SettingsHelper();
            SettingsHelper.TcomSettings setting = sh.LoadSetting();
            if (!string.IsNullOrEmpty(setting.SelectedAccount))
            {
                cboTcomAccount.SelectedItem = setting.SelectedAccount;
            }
            if (!string.IsNullOrEmpty(setting.SelectedPayment))
            {
                cboPaymentMethod.SelectedItem = setting.SelectedPayment;
            }
            if (!string.IsNullOrEmpty(setting.SelectedVAT))
            {
                cboVAT.SelectedItem = setting.SelectedVAT;
            }
        }

        private void SaveSettings()
        {
            SettingsHelper sh = new SettingsHelper();
            SettingsHelper.TcomSettings setting = new SettingsHelper.TcomSettings();
            setting.SelectedAccount = cboTcomAccount.Text;
            setting.SelectedPayment = cboPaymentMethod.Text;
            setting.SelectedVAT = cboVAT.Text;
            sh.SaveSettings(setting);
        }
        #endregion

        private void setDirtyAfterChanged(object sender, EventArgs e)
        {
            isDirty = true;
        }

        private void chkPrintFreeStyle_CheckedChanged(object sender, EventArgs e)
        {
            chkPrintInvoiceHeader.Visible = chkPrintFreeStyle.Checked;
            chkPrintCustomerInfo1.Visible = chkPrintFreeStyle.Checked;
            chkPrintCustomerInfo2.Visible = chkPrintFreeStyle.Checked;
            chkPrintCustomerInfo3.Visible = chkPrintFreeStyle.Checked;
            chkPrintCustomerInfo4.Visible = chkPrintFreeStyle.Checked;
            chkPrintCustomerInfo5.Visible = chkPrintFreeStyle.Checked;
            chkPrintInvoiceFooter.Visible = chkPrintFreeStyle.Checked;
            listView1.CheckBoxes = chkPrintFreeStyle.Checked;
            if (chkPrintFreeStyle.Checked)
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    item.Checked = true;
                }
            }
        }
    }
}
